﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using HYLightingEquipmentLay.SmokeDetectorLayout;
using HYView;
using RegionSplittor.Utils;
using SelectTypeClass;

namespace HYLightingEquipmentLay
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdFuncAeraSmokeLayout : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			commandData.Application.Application.FailuresProcessing += DeleteWarning.OnFailuresProcessing;
			Result result = 0;
			try
			{
				if (SwitchView.SetUsableView(CmdViewType.FloorPlan, commandData) != Autodesk.Revit.UI.Result.Succeeded)
				{
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				Document document = commandData.Application.ActiveUIDocument.Document;
				Document linkedDocByCurDoc = LinkDocumentOperator.GetLinkedDocByCurDoc(document, "A");
				Document linkedDocByCurDoc2 = LinkDocumentOperator.GetLinkedDocByCurDoc(document, "S");
				if (linkedDocByCurDoc2 == null || linkedDocByCurDoc == null)
				{
					YJKMessageBox.Show("项目中不存在建筑或结构的链接文件！");
					return Autodesk.Revit.UI.Result.Failed;
				}
				List<CurveLoop> userSelectedLoops = CurveloopsSelector.GetUserSelectedLoops(commandData, linkedDocByCurDoc);
				if (userSelectedLoops.Count > 0)
				{
					result = SmokeDetectorLayouter.FuncAeraSmokeLayout(commandData, userSelectedLoops, this._wndRevit);
				}
				else
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
			}
			catch (Exception)
			{
				result = Autodesk.Revit.UI.Result.Cancelled;
			}
			return result;
		}

		private YJKWindowHandle _wndRevit;
	}
}
